﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.Odbc;
using System.Timers;

namespace caculate
{
    public partial class Exc : Form
    {
        public static string[] EasyQuestions;
        public static string[] NormalQuestions;
        public static string[] HardQuestions;
        public static string[] Questions;
        public static int[] EasyAnswer;
        public static int[] NormalAnswer;
        public static int[] HardAnswer;
        public static int[] EasyAnswer2;
        public static int[] NormalAnswer2;
        public static int[] HardAnswer2;
        public static int[] Answer;
        public static int[] EYON;
        public static int[] NYON;
        public static int[] HYON;
        public static int[] YON;
        //public static int excercise_turn = 0;
        public static int control = 0;

        public Exc()
        {
            InitializeComponent();
            Random rad = new Random();
            int value1;
            int value2;
            int result;
            int key;
            string str1;
            string str2;
            string str3;
            int i;
            //简单
            EasyQuestions = new string[21];
            EasyAnswer = new int[21];
            for (i = 1; i <= 20; i++)
            {
                EasyAnswer[i] = -1;
            }
            for (i = 1; i <= 20; i++)
            {
                key = rad.Next(1, 100);
                if (key < 50)
                {
                    value1 = rad.Next(1, 100);
                    value2 = rad.Next(1, 100);
                    result = value1 + value2;
                    EasyAnswer[i] = result;
                    str1 = value1.ToString();
                    str2 = "+";
                    str3 = value2.ToString();
                    EasyQuestions[i] = str1 + str2 + str3;
                }
                else
                {
                    value1 = rad.Next(50, 100);
                    value2 = rad.Next(1, 50);
                    result = value1 - value2;
                    EasyAnswer[i] = result;
                    str1 = value1.ToString();
                    str2 = "-";
                    str3 = value2.ToString();
                    EasyQuestions[i] = str1 + str2 + str3;
                }
            }
            //中等
            NormalQuestions = new string[21];
            NormalAnswer = new int[21];
            for (i = 1; i <= 20; i++)
            {
                NormalAnswer[i] = -1;
            }
            for (i = 1; i <= 20; i++)
            {
                key = rad.Next(1, 400);
                if (key < 100)
                {
                    value1 = rad.Next(1, 200);
                    value2 = rad.Next(1, 200);
                    result = value1 + value2;
                    NormalAnswer[i] = result;
                    str1 = value1.ToString();
                    str2 = "+";
                    str3 = value2.ToString();
                    NormalQuestions[i] = str1 + str2 + str3;
                }
                else if (key < 200)
                {
                    value1 = rad.Next(100, 200);
                    value2 = rad.Next(1, 100);
                    result = value1 - value2;
                    NormalAnswer[i] = result;
                    str1 = value1.ToString();
                    str2 = "-";
                    str3 = value2.ToString();
                    NormalQuestions[i] = str1 + str2 + str3;
                }
                else if (key < 300)
                {
                    value1 = rad.Next(1, 10);
                    value2 = rad.Next(1, 50);
                    result = value1 * value2;
                    NormalAnswer[i] = result;
                    str1 = value1.ToString();
                    str2 = "×";
                    str3 = value2.ToString();
                    NormalQuestions[i] = str1 + str2 + str3;
                }
                else
                {
                    value1 = rad.Next(1, 50);
                    value2 = rad.Next(1, 10);
                    result = value1 * value2;
                    NormalAnswer[i] = result;
                    str1 = value1.ToString();
                    str2 = "×";
                    str3 = value2.ToString();
                    NormalQuestions[i] = str1 + str2 + str3;
                }
            }
            //困难
            HardQuestions = new string[21];
            HardAnswer = new int[21];
            for (i = 1; i <= 20; i++)
            {
                HardAnswer[i] = -1;
            }
            for (i = 1; i <= 20; i++)
            {
                key = rad.Next(1, 1000);
                if (key < 200)
                {
                    value1 = rad.Next(1, 200);
                    value2 = rad.Next(1, 200);
                    result = value1 + value2;
                    HardAnswer[i] = result;
                    str1 = value1.ToString();
                    str2 = "+";
                    str3 = value2.ToString();
                    HardQuestions[i] = str1 + str2 + str3;
                }
                else if (key < 400)
                {
                    value1 = rad.Next(100, 200);
                    value2 = rad.Next(1, 100);
                    result = value1 - value2;
                    HardAnswer[i] = result;
                    str1 = value1.ToString();
                    str2 = "-";
                    str3 = value2.ToString();
                    HardQuestions[i] = str1 + str2 + str3;
                }
                else if (key <550)
                {
                    value1 = rad.Next(1, 10);
                    value2 = rad.Next(1, 50);
                    result = value1 * value2;
                    HardAnswer[i] = result;
                    str1 = value1.ToString();
                    str2 = "×";
                    str3 = value2.ToString();
                    HardQuestions[i] = str1 + str2 + str3;
                }
                else if (key <700)
                {
                    value1 = rad.Next(1, 50);
                    value2 = rad.Next(1, 10);
                    result = value1 * value2;
                    HardAnswer[i] = result;
                    str1 = value1.ToString();
                    str2 = "×";
                    str3 = value2.ToString();
                    HardQuestions[i] = str1 + str2 + str3;
                }
                else
                {
                    value1 = rad.Next(10, 30);
                    value2 = rad.Next(1, 30);
                    result = value1;
                    value1 *= value2;
                    HardAnswer[i] = result;
                    str1 = value1.ToString();
                    str2 = "÷";
                    str3 = value2.ToString();
                    HardQuestions[i] = str1 + str2 + str3;
                }
            }
        }

        private void CheckAnswer_Click(object sender, EventArgs e)
        {
            //加入错题库
            if (control == 0)
            {
                //excercise_turn++;
                string conString = "DSN=test;UID=root;PWD=123456"; //ODBC的数据源名
                                                                    //建立连接
                OdbcConnection con1 = new OdbcConnection(conString);
                OdbcConnection con2 = new OdbcConnection(conString);
                con1.Open();
                con2.Open();

                YON = new int[21];
                Answer = new int[21];
                Questions = new string[21];
                if (MessageBox.Show("确定提交?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2)
     == DialogResult.OK)
                {
                    time.t = 1;//已使用时间归零
                    StartTime.Stop();
                    Executetime.Stop();//停止计时

                    children_operate.PublicValue4.is_exercised = 1;
                    //判断对错
                    if (mode.SelectedItem.ToString() == "简单")
                    {

                        egrade frm_egrade = new egrade();
                        frm_egrade.ShowDialog();
                        YON = EYON;
                        Answer = EasyAnswer;
                        Questions = EasyQuestions;
                        for (int i = 1; i < 21; i++)
                        {
                            if (YON[i] == 0)
                            {
                                string judge = "insert into problem values('" + Questions[i] + "','" +'='+ Answer[i] + "','"+ DateTime.Now.ToShortDateString().ToString() + "','"+Form1.PublicValue.chdaccount + "','" + Form1.PublicValue.chdclass + "')";
                                OdbcCommand com1 = new OdbcCommand(judge, con1);
                                int k = Convert.ToInt32(com1.ExecuteScalar());
                            }

                        }
                    }
                    else if (mode.SelectedItem.ToString() == "中等")
                    {
                        ngrade frm_ngrade = new ngrade();
                        frm_ngrade.ShowDialog();
                        YON = NYON;
                        Answer = NormalAnswer;
                        Questions = NormalQuestions;
                        for (int i = 1; i < 21; i++)
                        {
                            if (YON[i] == 0)
                            {
                                string judge = "insert into problem values('" + Questions[i] + "','" +'='+ Answer[i] + "','" + DateTime.Now.ToShortDateString().ToString() + "','" + Form1.PublicValue.chdaccount + "','" + Form1.PublicValue.chdclass + "')";
                                OdbcCommand com1 = new OdbcCommand(judge, con1);
                                int k = Convert.ToInt32(com1.ExecuteScalar());
                            }

                        }
                    }
                    else
                    {
                        hgrade frm_hgrade = new hgrade();
                        frm_hgrade.ShowDialog();
                        YON = HYON;
                        Answer = HardAnswer;
                        Questions = HardQuestions;
                        for (int i = 1; i < 21; i++)
                        {
                            if (YON[i] == 0)
                            {
                                string judge = "insert into problem values('" + Questions[i] + "','" +'='+ Answer[i] + "','" + DateTime.Now.ToShortDateString().ToString() + "','" + Form1.PublicValue.chdaccount + "','" + Form1.PublicValue.chdclass + "')";
                                OdbcCommand com1 = new OdbcCommand(judge, con1);
                                int k = Convert.ToInt32(com1.ExecuteScalar());
                            }

                        }
                    }
                    control = 1;
                    
                }
                
            }
            else if (control == 1)
            {
                MessageBox.Show("请勿重复提交！", "提示");
            }
            

            
        }

        public class MODE
        {
            public static EasyMode frm_easy; //= new EasyMode() { TopLevel = false, FormBorderStyle = FormBorderStyle.None };
            public static NormalMode frm_normal;// = new NormalMode() { TopLevel = false, FormBorderStyle = FormBorderStyle.None };
            public static HardMode frm_hard;//= new HardMode() { TopLevel = false, FormBorderStyle = FormBorderStyle.None };
        }

        public class time
        {
            public static int t = 1;//已使用时间（s）
            public static int control = 0;
        }

       

        private void Exc_Load(object sender, EventArgs e)
        {           
            //分别生成三种难度的随机算式题（每次调用该界面是重新生成）
            mode.Items.Add("简单");
            mode.Items.Add("中等");
            mode.Items.Add("困难");
            MODE.frm_easy = new EasyMode() { TopLevel = false, FormBorderStyle = FormBorderStyle.None };
            MODE.frm_normal = new NormalMode() { TopLevel = false, FormBorderStyle = FormBorderStyle.None };
            MODE.frm_hard = new HardMode() { TopLevel = false, FormBorderStyle = FormBorderStyle.None };

            //初始化ExecuteTime时间间隔
            Executetime.Interval = 1000;//间隔1s;
            Executetime.Enabled = false;
  
            }

        private void mode_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void choose_mode_Click(object sender, EventArgs e)
        {
            if (time.control == 0)
            {
                if (mode.Text != "")
                {
                    //时间控制
                    //设置要求时间
                    if (mode.Text == "简单")
                    {
                        StartTime.Interval = 300000;//执行时间间隔(毫秒) 5分钟
                        StartTime.Start();//启动计时
                        TimeLimite.Text = "300s";
                    }
                    else if (mode.Text == "中等")
                    {
                        StartTime.Interval = 360000;//6分钟
                        StartTime.Start();//启动计时
                        TimeLimite.Text = "360s";
                    }
                    else if (mode.Text == "困难")
                    {
                        StartTime.Interval = 420000;//7分钟
                        StartTime.Start();//启动计时
                        TimeLimite.Text = "420s";
                    }

                    //开始计时
                    Executetime.Start();



                    //显示题目
                    switch (mode.SelectedItem.ToString())
                    {
                        case "简单":
                            {
                                this.panel2.Controls.Clear();
                                //EasyMode frm_easy = new EasyMode() { TopLevel = false, FormBorderStyle = FormBorderStyle.None };
                                this.panel2.Controls.Add(MODE.frm_easy);
                                MODE.frm_easy.Show();

                            }
                            break;
                        case "中等":
                            {
                                this.panel2.Controls.Clear();
                                //NormalMode frm_normal = new NormalMode() { TopLevel = false, FormBorderStyle = FormBorderStyle.None };
                                this.panel2.Controls.Add(MODE.frm_normal);
                                MODE.frm_normal.Show();

                            }
                            break;
                        case "困难":
                            {
                                this.panel2.Controls.Clear();
                                //HardMode frm_hard = new HardMode() { TopLevel = false, FormBorderStyle = FormBorderStyle.None };
                                this.panel2.Controls.Add(MODE.frm_hard);
                                MODE.frm_hard.Show();

                            }
                            break;
                        default:
                            break;

                    }
                    ChangeEnable_false_choosemode();
                }
                else
                {
                    MessageBox.Show("请选择正确难易度！", "提示");
                }
            }
        }

        private void Panel2_Paint(object sender, PaintEventArgs e)
        {

        }

        private void count1_Tick(object sender, EventArgs e)
        {

        }

        private void Executetime_Tick(object sender, EventArgs e)
        {
            Usedtime.Text = Convert.ToString(time.t);
            time.t = time.t + 1;

        }
        public void ChangeEnable_true_checkanswer()
        {
            CheckAnswer.Enabled = true;
        }
        public void ChangeEnable_false_checkanswer()
        {
            CheckAnswer.Enabled = false;
        }

        public void ChangeEnable_true_choosemode()
        {
            choose_mode.Enabled = true;
        }
        public void ChangeEnable_false_choosemode()
        {
            choose_mode.Enabled = false;
        }

        private void StartTime_Tick(object sender, EventArgs e)
        {
            PauseStart.Enabled = false;
            this.ChangeEnable_false_checkanswer();
            //CheckAnswer.Enabled = false;
            //CheckAnswer_Click(null,null);
            time.control = 1;

            time.t = 1;//已使用时间归零
            Executetime.Stop();//停止计时器
            StartTime.Stop();
            MessageBox.Show("您已超过规定时间！强制提交答案", "提示");
            //加入错题库

            string conString = "DSN=caculate;UID=sa;PWD=sa123"; //ODBC的数据源名
                                                                    
                OdbcConnection con1 = new OdbcConnection(conString);//建立连接
                OdbcConnection con2 = new OdbcConnection(conString);
                con1.Open();
                con2.Open();

                YON = new int[21];
                Answer = new int[21];
                Questions = new string[21];
                /*if (MessageBox.Show("确定提交?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2)
     == DialogResult.OK)*/
               // {
                    children_operate.PublicValue4.is_exercised = 1;
                    //判断对错
                    if (mode.SelectedItem.ToString() == "简单")
                    {

                        egrade frm_egrade = new egrade();
                        frm_egrade.ShowDialog();
                        YON = EYON;
                        Answer = EasyAnswer;
                        Questions = EasyQuestions;
                        for (int i = 1; i < 21; i++)
                        {
                            if (YON[i] == 0)
                            {
                                string judge = "insert into problem values('" + Questions[i] + "','" + '=' + Answer[i] + "','" + DateTime.Now.ToShortDateString().ToString() + "','" + Form1.PublicValue.chdaccount + "','" + Form1.PublicValue.chdclass + "')";
                                OdbcCommand com1 = new OdbcCommand(judge, con1);
                                int k = Convert.ToInt32(com1.ExecuteScalar());
                            }

                        }
                    }
                    else if (mode.SelectedItem.ToString() == "中等")
                    {
                        ngrade frm_ngrade = new ngrade();
                        frm_ngrade.ShowDialog();
                        YON = NYON;
                        Answer = NormalAnswer;
                        Questions = NormalQuestions;
                        for (int i = 1; i < 21; i++)
                        {
                            if (YON[i] == 0)
                            {
                                string judge = "insert into problem values('" + Questions[i] + "','" + '=' + Answer[i] + "','" + DateTime.Now.ToShortDateString().ToString() + "','" + Form1.PublicValue.chdaccount + "','" + Form1.PublicValue.chdclass + "')";
                                OdbcCommand com1 = new OdbcCommand(judge, con1);
                                int k = Convert.ToInt32(com1.ExecuteScalar());
                            }

                        }
                    }
                    else
                    {
                        hgrade frm_hgrade = new hgrade();
                        frm_hgrade.ShowDialog();
                        YON = HYON;
                        Answer = HardAnswer;
                        Questions = HardQuestions;
                        for (int i = 1; i < 21; i++)
                        {
                            if (YON[i] == 0)
                            {
                                string judge = "insert into problem values('" + Questions[i] + "','" + '=' + Answer[i] + "','" + DateTime.Now.ToShortDateString().ToString() + "','" + Form1.PublicValue.chdaccount + "','" + Form1.PublicValue.chdclass + "')";
                                OdbcCommand com1 = new OdbcCommand(judge, con1);
                                int k = Convert.ToInt32(com1.ExecuteScalar());
                            }

                        }
                    }
                //}
 
            

           
        }

        private void PauseStart_Click(object sender, EventArgs e)
        {
            if (mode.Text == "简单")
            {
                if (PauseStart.Text == "暂停")
                {
                    PauseStart.Text = "开始";
                    StartTime.Interval = 301000 - (time.t * 1000);
                    StartTime.Stop();
                    Executetime.Stop();
                    this.panel2.Hide();
                }
                else if (PauseStart.Text == "开始")
                {
                    PauseStart.Text = "暂停";
                    StartTime.Start();
                    Executetime.Start();
                    this.panel2.Show();
                }
            }
            else if (mode.Text == "中等")
            {
                if (PauseStart.Text == "暂停")
                {
                    PauseStart.Text = "开始";
                    StartTime.Interval = 361000 - (time.t * 1000);
                    StartTime.Stop();
                    Executetime.Stop();
                    this.panel2.Hide();
                }
                else if (PauseStart.Text == "开始")
                {
                    PauseStart.Text = "暂停";
                    StartTime.Start();
                    Executetime.Start();
                    this.panel2.Show();
                }
            }
            else if (mode.Text == "困难")
            {
                if (PauseStart.Text == "暂停")
                {
                    PauseStart.Text = "开始";
                    StartTime.Interval = 421000 - (time.t * 1000);
                    StartTime.Stop();
                    Executetime.Stop();
                    this.panel2.Hide();
                }
                else if (PauseStart.Text == "开始")
                {
                    PauseStart.Text = "暂停";
                    StartTime.Start();
                    Executetime.Start();
                    this.panel2.Show();
                }
            }
        }
    }
}
